package com.ruoyi.finance.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.finance.domain.Industry;
import com.ruoyi.finance.service.IIndustryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.finance.domain.AccountSet;
import com.ruoyi.finance.service.IAccountSetService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 账套系统Controller
 *
 * @author ruoyi
 * @date 2025-02-24
 */
@RestController
@RequestMapping("/finance/set")
public class AccountSetController extends BaseController
{
    @Autowired
    private IAccountSetService accountSetService;

    @Autowired
    private IIndustryService iIndustryService;
    /**
     * 查询账套系统列表
     */
    @PreAuthorize("@ss.hasPermi('finance:set:list')")
    @GetMapping("/list")
    public TableDataInfo list(AccountSet accountSet)
    {
        startPage();
        List<AccountSet> list = accountSetService.selectAccountSetList(accountSet);
        return getDataTable(list);
    }

    @GetMapping("/tolist")
    public AjaxResult tolist(){

        List<Industry> industries = iIndustryService.selectIndustryList(null);
        return AjaxResult.success(industries);
    }

    /**
     * 查询账套列表 -查询每个用户的默认账套
     */
    @PreAuthorize("@ss.hasPermi('finance:set:list')")
    @GetMapping("/listAccountSetAll")
    public AjaxResult listAccountSetAll()
    {
        List<AccountSet> list = accountSetService.listAccountSetAllByUserId();
        return  success(list);
    }


    @PreAuthorize("@ss.hasPermi('finance:set:edit')")
    @PutMapping("/checkDefaultAccountSet/{accountId}")
    public AjaxResult checkDefaultAccountSet(@PathVariable("accountId") Long accountId){
        if (accountId == null){
            return AjaxResult.error("参数错误");
        }

        return toAjax(accountSetService.checkDefaultAccountSet(accountId));
    }

    /**
     * 导出账套系统列表
     */
    @PreAuthorize("@ss.hasPermi('finance:set:export')")
    @Log(title = "账套系统", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccountSet accountSet)
    {
        List<AccountSet> list = accountSetService.selectAccountSetList(accountSet);
        ExcelUtil<AccountSet> util = new ExcelUtil<AccountSet>(AccountSet.class);
        util.exportExcel(response, list, "账套系统数据");
    }

    /**
     * 获取账套系统详细信息
     */
    @PreAuthorize("@ss.hasPermi('finance:set:query')")
    @GetMapping(value = "/{accountId}")
    public AjaxResult getInfo(@PathVariable("accountId") Long accountId)
    {
        return success(accountSetService.selectAccountSetByAccountId(accountId));
    }

    /**
     * 新增账套系统
     */
    @PreAuthorize("@ss.hasPermi('finance:set:add')")
    @Log(title = "账套系统", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AccountSet accountSet)
    {
        return toAjax(accountSetService.insertAccountSet(accountSet));
    }

    /**
     * 修改账套系统
     */
    @PreAuthorize("@ss.hasPermi('finance:set:edit')")
    @Log(title = "账套系统", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccountSet accountSet)
    {
        return toAjax(accountSetService.updateAccountSet(accountSet));
    }

    /**
     * 删除账套系统
     */
    @PreAuthorize("@ss.hasPermi('finance:set:remove')")
    @Log(title = "账套系统", businessType = BusinessType.DELETE)
	@DeleteMapping("/{accountIds}")
    public AjaxResult remove(@PathVariable Long[] accountIds)
    {

        return toAjax(accountSetService.deleteAccountSetByAccountIds(accountIds));

    }

}
